<template>
    <el-card shadow="never" :body-style="{ padding: '16px' }">
        <div class="ad_header">
            <div class="ad_title">{{ title }}</div>
            <div class="ad_tab">
                <!-- Radio group to switch tabs -->
                <el-radio-group v-model="currentTab" v-if="tab">
                    <el-radio-button v-for="(item, index) in tab" :key="index" :value="item.value">
                        {{ item.value }}
                    </el-radio-button>
                </el-radio-group>
            </div>
        </div>
        <div class="tip_info">
            <!-- Dynamically show content based on the selected tab -->
            <div class="tip_pic">
                <slot name="chart" :currentTab="currentTab"></slot>
            </div>
            <div v-if="details" class="tip_detail">
                <div v-for="(item, index) in details" :key="index" class="tip_c">
                    <div class="tip_c_title">{{ item.title }}</div>
                    <div class="tip_c_detail" :style="{ color: item.color }">
                        <span>{{ item.value }}</span>
                        <el-icon>
                            <component v-if="item.icon" :is="item.icon"></component>
                        </el-icon>
                    </div>
                </div>
            </div>
        </div>
    </el-card>
</template>

<script setup name="TipCard">
import { ref, watch } from "vue";

// Define props
const props = defineProps({
    title: {
        type: String,
        required: true,
    },
    details: {
        type: Array,
        required: false,
    },
    tab: {
        type: Array,
        required: false,
        default: () => [], // 默认值为空数组
    },
});
const currentTab = ref(null);
watch(
    () => props.tab,
    (newTab) => {
        if (newTab?.length > 0) {
            currentTab.value = newTab[0].value; // 默认选中第一个 tab
        }
    },
    { immediate: true } // 立即执行，确保初始值设置正确
);
</script>


<style lang="scss" scoped>
.ad_header {
    display: flex;

    .ad_title {
        font-size: 14px;
        font-weight: 600;
        font-family: "Montserrat";
        color: #333333;
        display: flex;
        align-items: center;
        padding-left: 14px;
        position: relative;
        margin-bottom: 16px;
        flex: 1;

        &:before {
            content: "";
            position: absolute;
            width: 4px;
            height: 12px;
            background: #0783ff;
            left: 0;
            margin: auto;
        }
    }

    .ad_tab {
        flex: 1;
        display: flex;
        justify-content: flex-end;
    }
}

.tip_info {
    display: flex;

    .tip_pic {
        flex: 2;
    }

    .tip_detail {
        flex: 1;
        padding-top: 0px;
        padding-right: 12px;
        padding-left: 20px;
        // border-left: 1px solid #ececec;

        .tip_c {
            display: flex;
            align-items: center;
            padding: 6px 0;

            .tip_c_title {
                text-align: left;
                font-weight: 400;
                flex: 2;
                font-size: 13px;
                color: #333333;
            }

            .tip_c_detail {
                flex: 1;
                font-size: 13px;
                font-weight: 600;
                display: flex;
                align-items: flex-start;
                justify-content: flex-end;
            }
        }
    }
}
</style>